package org.csu.backofficemanagersys.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.csu.backofficemanagersys.domain.Account;
import org.csu.backofficemanagersys.domain.SignOn;
import org.csu.backofficemanagersys.mapper.AccountMapper;
import org.csu.backofficemanagersys.mapper.SignOnMapper;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class AccountService {
    final AccountMapper accountMapper;
    final SignOnMapper signOnMapper;

    public AccountService(AccountMapper accountMapper,SignOnMapper signOnMapper) {
        this.accountMapper = accountMapper;
        this.signOnMapper = signOnMapper;
    }

    public List<Account> findAccountLike(String userid,String email,String phone){
        //当condition为false时，当前这个条件方法不会生效
        List<Account> accounts = accountMapper.selectList(new QueryWrapper<Account>().like(userid!=null,"userid",userid)
                                                                                     .like(email!=null,"email",email)
                                                                                     .like(phone!=null,"phone",phone));
        for(Account account : accounts){
            SignOn signOn;
            QueryWrapper<SignOn> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("username",account.getUserid());
            signOn = signOnMapper.selectOne(queryWrapper);
            account.setPassword(signOn.getPassword());
        }
        return accounts;
    }
    public Account getAccountByUsername(String username){
        Account account;
        SignOn signOn;
        QueryWrapper<SignOn> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username",username);
        signOn = signOnMapper.selectOne(queryWrapper);
        QueryWrapper<Account> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("userid",username);
        account = accountMapper.selectOne(queryWrapper1);
        account.setPassword(signOn.getPassword());
        return account;
    }
    public List<Account> getAllAccount(){
        List<Account> accountList = accountMapper.selectList(null);
        for(Account account : accountList){
            SignOn signOn;
            QueryWrapper<SignOn> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("username",account.getUserid());
            signOn = signOnMapper.selectOne(queryWrapper);
            account.setPassword(signOn.getPassword());
        }
        return accountList;
    }
    public Boolean updateAccount(Account account){
        return accountMapper.update(account,new UpdateWrapper<Account>().eq("userid",account.getUserid()))!=0;
    }
    public Boolean ReSetPassword(String username){
        UpdateWrapper<SignOn> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("username",username).set("password","111111");
        return signOnMapper.update(null,updateWrapper)!= 0;
    }

}
